package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMingXiShouLi;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppMingXiShouLiDao.java
 * @Description TODO
 * @createTime 2021年11月02日 15:42:00
 */
@Repository
public class AppMingXiShouLiDao extends BaseDao<AppMingXiShouLi> {

    public void saveOrUpdate(AppMingXiShouLi appMingXiShouLi) {
        if (appMingXiShouLi.getId() == null || appMingXiShouLi.getId() == 0) {
            appMingXiShouLi.setId(this.add(appMingXiShouLi));
        } else {
            this.update(appMingXiShouLi);
        }
    }

    /**
     * 更新礼物信息
     */
    public void updateGiftData(Integer giftid,String picture,String giftname) {
        if(StringEx.isNull(giftid)){
            return ;
        }
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuffer sql =new StringBuffer(" update app_mingxi_shouli set giftpic=? ") ;
        paramList.add(picture);
        sql.append(" ,giftname=? ");
        paramList.add(giftname);
        sql.append(" where giftid=? ");
        paramList.add(giftid);
        this.update(sql.toString(),paramList);
    }

    //查找用户收礼明细
    public List<AppMingXiShouLi> findList(Integer userid, SplitPageBean splitPageBean) {
        if (StringEx.isNull(userid)) {
            return null;
        }
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append(" select * from app_mingxi_shouli where userid = ? ");
        list.add(userid);
        str.append(" order by createtime desc ");
        String sql = str.toString();
        return this.queryList(sql, list, AppMingXiShouLi.class, splitPageBean);
    }

    //查找用户收礼明细不分页
    public List<AppMingXiShouLi> selectList(Integer userid) {
        if (StringEx.isNull(userid)) {
            return null;
        }
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append(" select * from app_mingxi_shouli where userid = ? ");
        list.add(userid);
        str.append(" order by createtime desc ");
        String sql = str.toString();
        return this.queryList(sql, list, AppMingXiShouLi.class);
    }


    //查找指定时间获取金豆的排行榜
    public List<AppMingXiShouLi> leaderBoard(String startTime,String endTime) {
        if (StringEx.isNull(startTime)) {
            return null;
        }
        if (StringEx.isNull(endTime)) {
            return null;
        }
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select *,sum(fujianum) as total from app_mingxi_shouli  where createtime >= ? and createtime <= ? GROUP BY userid  ORDER BY total desc");
        list.add(startTime);
        list.add(endTime);
        String sql = str.toString();
        return this.queryList(sql, list, AppMingXiShouLi.class);
    }

    //查找用户送礼明细
    public List<AppMingXiShouLi> findSongli(Integer userid, SplitPageBean splitPageBean) {
        if (StringEx.isNull(userid)) {
            return null;
        }
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select * from app_mingxi_shouli where 1=1 ");
        str.append(" and userid = ? ");
        list.add(userid);
        String sql = str.toString();
        return this.queryList(sql, list, AppMingXiShouLi.class, splitPageBean);
    }

    public List<AppMingXiShouLi> list(Map<String, Object> params) {
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select * from app_mingxi_shouli where 1=1 ");
        if (StringEx.isNotNull(params.get("userid"))) {
            str.append(" and userid = ? ");
            list.add(params.get("userid"));
        }
        if(params.containsKey("userids") && StringEx.isNotNull(params.get("userids"))){
            str.append(" and userid in ( ");
            str.append(params.get("userids"));
            str.append(" ) ");
        }
        if (StringEx.isNotNull(params.get("nick"))) {
            str.append(" and nick like '%").append(params.get("nick")).append("%'");
        }
        if (StringEx.isNotNull(params.get("phone"))) {
            str.append(" and phone like '%").append(params.get("phone")).append("%'");
        }
        if (StringEx.isNotNull(params.get("giftname"))) {
            str.append(" and giftname like '%").append(params.get("giftname")).append("%'");
        }
        if(params.containsKey("today") && StringEx.isNotNull(params.get("today"))) {
            str.append(" and to_days(createtime) = to_days(now())");
        }
        if(StringEx.isNotNull(params.get("startDate"))){
            str.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  >=  ? ");
            list.add(params.get("startDate"));
        }
        if(StringEx.isNotNull(params.get("endDate"))){
            str.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  <=  ? ");
            list.add(params.get("endDate"));
        }
        str.append(" order by createtime desc ");
        String sql = str.toString();
        return this.queryList(sql, list, AppMingXiShouLi.class);
    }

    public List<AppMingXiShouLi> list(Map<String, Object> params, SplitPageBean splitPageBean) {
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select * from app_mingxi_shouli where 1=1 ");
        if (StringEx.isNotNull(params.get("userid"))) {
            str.append(" and userid = ? ");
            list.add(params.get("userid"));
        }
        if(params.containsKey("userids") && StringEx.isNotNull(params.get("userids"))){
            str.append(" and userid in ( ");
            str.append(params.get("userids"));
            str.append(" ) ");
        }
        if (StringEx.isNotNull(params.get("nick"))) {
            str.append(" and nick like '%").append(params.get("nick")).append("%'");
        }
        if (StringEx.isNotNull(params.get("phone"))) {
            str.append(" and phone like '%").append(params.get("phone")).append("%'");
        }
        if (StringEx.isNotNull(params.get("giftname"))) {
            str.append(" and giftname like '%").append(params.get("giftname")).append("%'");
        }
        if(StringEx.isNotNull(params.get("startDate"))){
            str.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  >=  ? ");
            list.add(params.get("startDate"));
        }
        if(StringEx.isNotNull(params.get("endDate"))){
            str.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  <=  ? ");
            list.add(params.get("endDate"));
        }
        str.append(" order by createtime desc ");
        String sql = str.toString();
        return this.queryList(sql, list, AppMingXiShouLi.class, splitPageBean);
    }


    public List<AppMingXiShouLi> listAfter(Map<String, Object> params) {

        List<Object> paramList = new ArrayList<>();

        StringBuilder str = new StringBuilder();

        str.append("select * from app_mingxi_shouli where 1=1 ");
        //锁定用户
        if(params.containsKey("userids") && StringEx.isNotNull(params.get("userids"))){
            str.append(" and userid in ( ");
            str.append(params.get("userids"));
            str.append(" ) ");
        }
        //
        if(params.containsKey("today") && StringEx.isNotNull(params.get("today"))) {
            str.append(" and createtime like ? " );
            paramList.add("%" + params.get("today") + "%");
        }
        if(params.containsKey("alltoday") && StringEx.isNotNull(params.get("alltoday"))) {
            str.append(" and createtime <= ? " );
            paramList.add(params.get("alltoday"));
        }

        String sql = str.toString();
        return this.queryList(sql, paramList, AppMingXiShouLi.class);
    }


    public List<AppMingXiShouLi> getGiftIdByUserId(Integer id,String time) {
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select * from app_mingxi_shouli where userid = ?");
        list.add(id);
        str.append(" and createtime >= ?");
        list.add(time);
        String sql = str.toString();
        return this.queryList(sql,list,AppMingXiShouLi.class);
    }

    public List<AppMingXiShouLi> getOneDayGiftIdByUserId(Integer id) {
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select * from app_mingxi_shouli where userid = ?");
        list.add(id);
        str.append(" and createtime >= DATE_FORMAT(now(), '%y-%m-%d 00:00:00') and createtime <= DATE_FORMAT(now(),'%y-%m-%d 23:59:59')");
        String sql = str.toString();
        return this.queryList(sql,list,AppMingXiShouLi.class);
    }

    public List<AppMingXiShouLi> getsevenDayRanking(Integer id,String time){
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select * from app_mingxi_shouli where userid = ?");
        list.add(id);
        str.append(" and createtime >= ?");
        list.add(time);
        String sql = str.toString();
        return this.queryList(sql,list,AppMingXiShouLi.class);
    }


    public List<AppMingXiShouLi> getDayRanking(Map<String, Object> params)
    {
        ArrayList<Object> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select sl.*,sum(ifnull(sl.fujianum,0)) as totalMoney from app_mingxi_shouli as sl join app_user as user");
        sb.append(" where to_days(sl.createtime) = to_days(now())");
        if(params.containsKey("hiddenUserIds") && StringEx.isNotNull(params.get("hiddenUserIds"))) {
            sb.append(" AND user.id not in ("+ params.get("hiddenUserIds") +")");
        }
        sb.append(" and user.id=sl.userId and user.sex=?");
        sb.append(" GROUP BY sl.userId ORDER BY totalMoney desc limit 100");
        list.add(params.get("sex"));
        return this.queryList(sb.toString(), list, AppMingXiShouLi.class);
    }


    public List<AppMingXiShouLi> getWeekRanking(Map<String, Object> params)
    {
        ArrayList<Object> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select sl.*,sum(ifnull(sl.fujianum,0)) as totalMoney from app_mingxi_shouli as sl join app_user as user");
        sb.append(" where YEARWEEK( date_format(  sl.createtime,'%Y-%m-%d' ) ) = YEARWEEK( now() ) ");
        if(params.containsKey("hiddenUserIds") && StringEx.isNotNull(params.get("hiddenUserIds"))) {
            sb.append(" AND user.id not in ("+ params.get("hiddenUserIds") +")");
        }
        sb.append(" and user.id=sl.userId and user.sex=?");
        sb.append(" GROUP BY sl.userId ORDER BY totalMoney desc limit 100");
        list.add(params.get("sex"));
        return this.queryList(sb.toString(),list,AppMingXiShouLi.class);
    }


    public List<AppMingXiShouLi> getMonthRanking(Map<String, Object> params)
    {
        ArrayList<Object> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select sl.*,sum(ifnull(sl.fujianum,0)) as totalMoney from app_mingxi_shouli as sl join app_user as user");
        sb.append(" where DATE_FORMAT( sl.createtime, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )");
        if(params.containsKey("hiddenUserIds") && StringEx.isNotNull(params.get("hiddenUserIds"))) {
            sb.append(" AND user.id not in ("+ params.get("hiddenUserIds") +")");
        }
        sb.append(" and user.id=sl.userId and user.sex=?");
        sb.append(" GROUP BY sl.userId ORDER BY totalMoney desc limit 100");
        list.add(params.get("sex"));
        return this.queryList(sb.toString(),list,AppMingXiShouLi.class);
    }



}
